package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.SysPtFinalScoreAddBo;
import com.ruoyi.system.bo.SysPtFinalScoreQueryBo;
import com.ruoyi.system.bo.SysPtFinalScoreEditBo;
import com.ruoyi.system.domain.SysPtFinalScore;
import com.ruoyi.system.mapper.SysPtFinalScoreMapper;
import com.ruoyi.system.vo.SysPtFinalScoreVo;
import com.ruoyi.system.service.ISysPtFinalScoreService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 最终成绩管理Service业务层处理
 *
 * @author 莫修
 * @date 2021-11-28
 */
@Service
public class SysPtFinalScoreServiceImpl extends ServiceImpl<SysPtFinalScoreMapper, SysPtFinalScore> implements ISysPtFinalScoreService {

    @Override
    public SysPtFinalScoreVo queryById(Long classId){
        return getVoById(classId, SysPtFinalScoreVo.class);
    }

    @Override
    public TableDataInfo<SysPtFinalScoreVo> queryPageList(SysPtFinalScoreQueryBo bo) {
        PagePlus<SysPtFinalScore, SysPtFinalScoreVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysPtFinalScoreVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysPtFinalScoreVo> queryList(SysPtFinalScoreQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysPtFinalScoreVo.class);
    }

    private LambdaQueryWrapper<SysPtFinalScore> buildQueryWrapper(SysPtFinalScoreQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysPtFinalScore> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getClassId() != null, SysPtFinalScore::getClassId, bo.getClassId());
        lqw.eq(bo.getUserId() != null, SysPtFinalScore::getUserId, bo.getUserId());
        lqw.eq(bo.getFinalScore() != null, SysPtFinalScore::getFinalScore, bo.getFinalScore());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysPtFinalScoreAddBo bo) {
        SysPtFinalScore add = BeanUtil.toBean(bo, SysPtFinalScore.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysPtFinalScoreEditBo bo) {
        SysPtFinalScore update = BeanUtil.toBean(bo, SysPtFinalScore.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysPtFinalScore entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
